package com.iceclean.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iceclean.po.Message;
import com.iceclean.po.User;
import com.iceclean.service.MessageService;
import com.iceclean.serviceImpl.MessageServiceImpl;
import com.iceclean.util.ServiceAgent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-09
 *
 * 接收所有与消息操作有关的请求
 */
public class MessageServlet extends BaseServlet {
    /**
     * 消息服务的动态代理
     */
    MessageService messageService = (MessageService) new ServiceAgent(new MessageServiceImpl()).getAgent();

    /**
     * 创建消息
     */
    public void createMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        // 获取发送者
        User user = (User) request.getSession().getAttribute("user");

        // 获取发送者id、接收者id、消息的内容以及消息的类别
        int receiverId = Integer.parseInt(request.getParameter("receiverId"));
        String messageContent = request.getParameter("messageContent");
        int messageClass = Integer.parseInt(request.getParameter("messageClass"));

        // 获取可能存在的消息自带超链接
        String messageHref = request.getParameter("messageHref");

        System.out.println("收到了：" + messageContent);

        // 创建新消息并获取消息实体
        Message message = messageService.createMessage(user.getUserId(), receiverId, messageContent, messageClass, messageHref);

        // 返回消息实体给前端
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(message));
    }

    /**
     * 按页数获取指定类型的消息
     */
    public void getAllMessageByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取绑定的用户
        User user = (User) request.getSession().getAttribute("user");

        // 获取消息类别，以及页数消息
        int messageClass = Integer.parseInt(request.getParameter("messageClass"));
        int page = Integer.parseInt(request.getParameter("page"));
        int num = Integer.parseInt(request.getParameter("num"));

        // 查询消息
        List<Message> messageList = messageService.findMessageByPage(user.getUserId(), messageClass, page, num);
        System.out.println(messageList);
        // 返回给前端
        ObjectMapper mapper = new ObjectMapper();
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(mapper.writeValueAsString(messageList));
    }

    /**
     * 获取绑定用户目标消息总类的数量
     */
    public void getAllMessageForNumber(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取绑定的用户
        User user = (User) request.getSession().getAttribute("user");

        // 获取消息种类
        int messageClass = Integer.parseInt(request.getParameter("messageClass"));

        // 查询数量并返回给前端
        int number = messageService.getAllMessageForNumber(user.getUserId(), messageClass);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(""+number);

    }
}
